package com.dtruth.dataflow.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;

import com.dtruth.dataflow.model.Fe_class_sample_text;
import com.dtruth.dataflow.util.MyLoggerFactory;

/**
 * 与分类相关的数据库操作
 * 
 * @author 岳海亮
 * @email yhl@feheadline.com
 * @date 2014年12月9日
 */
public class ClassifierDAO extends BaseDAO<Fe_class_sample_text> {

	private static Logger logger = MyLoggerFactory
			.getLogger(ClassifierDAO.class);

	public void save(String tableName, String aclass, String bclass,
			String title, String article, String acctname) {
		PreparedStatement pstmt = null;
		try {
			String sql = "REPLACE INTO `"
					+ tableName
					+ "`(channel,subchannel,title,content,acct_login_name) VALUES (?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, aclass);
			pstmt.setString(2, bclass);
			pstmt.setString(3, title);
			pstmt.setString(4, article);
			pstmt.setString(5, acctname);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			logger.error(ExceptionUtils.getStackTrace(e));
		} finally {
			try {
				close(pstmt, null);
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
		}
	}

	public void close() {
		try {
			super.conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param channel
	 * @param subchannel
	 * @return
	 */
	public List<String> listByChannel(String tableName, String channel,
			String subchannel) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<String> list = new ArrayList<String>();
		try {
			String sql = "select title,content from `" + tableName
					+ "` where channel=? and subchannel=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, channel);
			pstmt.setString(2, subchannel);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				String title = rs.getString(1);
				String article = rs.getString(2);
				list.add(title + "," + title + "," + title + "," + article);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				close(pstmt, rs);
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
		}
		return list;
	}

	/**
	 * 
	 * @return
	 */
	public List<String> getAllChannels(String table) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<String> list = new ArrayList<String>();
		try {
			String sql = "select channel,subchannel from `" + table
					+ "` group by channel,subchannel";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				String channel = rs.getString(1);
				String subchannel = "";
				if (rs.getString(2) != null) {
					subchannel = rs.getString(2);
				}
				list.add(channel + "@" + subchannel);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				close(pstmt, rs);
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
		}
		return list;
	}

	/**
	 * 
	 * @param channel
	 * @param subchannel
	 * @return
	 */
	public List<Fe_class_sample_text> list(String tableName) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Fe_class_sample_text> list = new ArrayList<Fe_class_sample_text>();
		try {
			String sql = "select id,channel,subchannel,title,content from `"
					+ tableName + "`";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				String id = rs.getString("id");
				String channel = rs.getString("channel");
				String subchannel = rs.getString("subchannel");
				String title = rs.getString("title");
				String content = rs.getString("content");
				Fe_class_sample_text fcst = new Fe_class_sample_text();
				fcst.setId(id);
				fcst.setChannel(channel);
				fcst.setSubchannel(subchannel);
				fcst.setTitle(title);
				fcst.setContent(content);
				list.add(fcst);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				close(pstmt, rs);
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
		}
		return list;
	}

	public int updateChannel(String tableName, String lid, String newChannel,
			String newSubChannel) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			String sql = "update `" + tableName
					+ "` set channel=?,subchannel=? where id=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, newChannel);
			pstmt.setString(2, newSubChannel);
			pstmt.setString(3, lid);
			return pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				close(pstmt, rs);
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
		}
		return 0;
	}
}
